import type { DictType, DictData, DictQueryParams, DictTypeForm, DictDataForm } from '@/types/dict';
import { http } from '@/utils/http'

// 字典类型 API
export const dictTypeApi = {
  // 获取字典类型列表
  getList(params: DictQueryParams) {
    return http.get<{ rows: DictType[]; total: number }>('/api/dict/dictTypeList', { params });
  },

  // 获取字典类型详情
  getDetail(dictId: number) {
    return http.get<DictType>(`/api/dict/dictTypeDetail/${dictId}`);
  },

  // 新增字典类型
  add(data: DictTypeForm) {
    return http.post('/api/dict/addDictType', data);
  },

  // 修改字典类型
  update(data: DictTypeForm) {
    return http.put('/api/dict/updateDictType', data);
  },

  // 删除字典类型
  delete(dictIds: number[]) {
    //判断不是数组， 就不用join了 怎么写
    if(!Array.isArray(dictIds)) {
      return http.post('/api/dict/deleteDictType',{'dictIds':dictIds});
    }else{
      return http.post('/api/dict/deleteDictType',{'dictIds':dictIds.join(',')});
    }
  },

  // 刷新字典缓存
  refreshCache() {
    return http.delete('/api/dict/refreshDictCache');
  },

  // 获取字典类型选项列表
  getOptionSelect() {
    return http.get<DictType[]>('/api/dict/dictTypeOptions');
  }
};

// 字典数据 API
export const dictDataApi = {
  // 获取字典数据列表
  getList(params: DictQueryParams) {
    return http.get<{ rows: DictData[]; total: number }>('/api/dict/dictDataList', { params });
  },

  // 根据字典类型获取字典数据
  getByType(dictType: string) {
    return http.get<DictData[]>(`/api/dict/dictDataByType/${dictType}`);
  },

  // 获取字典数据详情
  getDetail(dictCode: number) {
    return http.get<DictData>(`/api/dict/dictDataDetail/${dictCode}`);
  },

  // 新增字典数据
  add(data: DictDataForm) {
    return http.post('/api/dict/addDictData', data);
  },

  // 修改字典数据
  update(data: DictDataForm) {
    return http.put('/api/dict/updateDictData', data);
  },

  // 删除字典数据
  delete(dictCodes: number[]) {
    //判断不是数组， 就不用join了 怎么写
    if(!Array.isArray(dictCodes)) {
      return http.post('/api/dict/deleteDictData',{'dictCodes':dictCodes});
    }else{
      return http.post('/api/dict/deleteDictData',{'dictCodes':dictCodes.join(',')});
    }
  }
};